package com.huluobo.a2208parkdemo.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208parkdemo.R
import com.huluobo.a2208parkdemo.databinding.ActivityAddRepairBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding, RepairViewModel>() {
    override fun initData() {
        vm.addRepairSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun initView() {
        binding.addRepairCommit.setOnClickListener {
            val descString = binding.addRepairEt.text.toString()
            if (descString.isNotEmpty()) {
                val timeString = getTime()

                val map = mutableMapOf<String, String>()
                map[Const.PARAM_REPAIR_DESC] = descString
                map[Const.PARAM_REPAIR_TIME] = timeString

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.addRepair(body)
            }

        }
    }
}